#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int N = 5e4 + 10;
int n;
int ret[N];
struct node
{
    int x;
    int y;
    int z;
    bool operator<(const node& f)const
    {
        return x > f.x;
    }
}a[N];
bool cmp(node& s1, node& s2)
{
    return s1.x < s2.x;
}
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i].x >> a[i].y;
        a[i].z = i;
    }
    sort(a + 1, a + 1 + n, cmp);
    priority_queue<node> as;
    ret[a[1].z] = 1;
    as.push({ a[1].y,1 });
    int num = 1;
    for (int i = 2; i <= n; i++)
    {
        int l = a[i].x;
        int r = a[i].y;
        if (l <= as.top().x)
        {
            num++;
            ret[a[i].z] = num;
            as.push({ r,num });
        }
        else
        {
            node aq = as.top();
            as.pop();
            ret[a[i].z] = aq.y;
            as.push({ a[i].y,aq.y });
        }
    }
    cout << num << endl;
    for (int i = 1; i <= n; i++)
    {
        cout << ret[i] << endl;
    }
    return 0;
}